package com.jilujia.domain.question.repository;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.question.QuestionTag;
import com.jilujia.framework.domain.JpaBusinessRepository;

@Repository
@Transactional(readOnly = true)
public interface QuestionTagRepository extends JpaBusinessRepository<Long, QuestionTag> {
	@Query("select qt from QuestionTag qt where qt.name = :tagName")
	QuestionTag findQuestionTagByName(@Param("tagName") String tagName);

	@Query("select qt from QuestionTag qt, QuestionTagRelation r where qt.id = r.tagId and r.questionId = :questionId")
	List<QuestionTag> findQuestionTagsByQuestionId(@Param("questionId") Long questionId);

	@Query("select t from QuestionTag t, QuestionTagRelation r1, QuestionTagRelation r2, Question q	where r1.tagId = :tagId and r1.questionId = q.id and q.id = r2.questionId and r2.tagId = t.id group by t.id	having t.id <> :tagId order by count(t.id) desc")
	List<QuestionTag> findAssociatedQuestionTags(@Param("tagId") Long tagId, Pageable pageable);
}
